// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: leon

#pragma once

#include <string>

#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>

#include "coin/proto/coin_market_enums.pb.h"
#include "coin2/base/conversion.h"
#include "coin2/exchange/base/feed/update.h"
#include "coin2/exchange/base/feed_util/book_builder.h"
#include "coin2/exchange/base/feed_util/trade.h"

namespace impl {
using coin2::exchange::base::market::ExchangeType;
using coin2::exchange::base::market::MarketType;

class KorbitFeedUpdateParser {
 public:
  static void ParseSnapshot(BookBuilder* book_builder_, const rapidjson::Value& book_json) {
    int64_t timestamp = std::llround(stod(book_json["timestamp"].GetString()) * 1'000'000LL);
    auto tx = book_builder_->NewTransaction(timestamp);
    tx.Clear();

    for (const auto& ask : book_json["asks"].GetArray()) {
      double price = stod(ask["price"].GetString());
      double size = stod(ask["amount"].GetString());
      tx.UpdateAsk(price, size);
    }

    for (const auto& bid : book_json["bids"].GetArray()) {
      double price = stod(bid["price"].GetString());
      double size = stod(bid["amount"].GetString());
      tx.UpdateBid(price, size);
    }
  }

  static void ParseTrade(const rapidjson::Value& transaction, Trade* trade) {
    int64_t timestamp = std::llround(stod(transaction["timestamp"].GetString()) * 1'000'000LL);
    trade->price = stod(transaction["price"].GetString());
    trade->fill_qty = std::stod(transaction["amount"].GetString());
    trade->side = ParseTradeSide(transaction);
    trade->timestamp = timestamp;
  }

  static coin2::exchange::feed::TradeSide ParseTradeSide(const rapidjson::Value& transaction) {
    const std::string& side = transaction["taker"].GetString();
    if (side == "sell") {
      return coin2::exchange::feed::TRADE_SELL_SIDE;
    } else if (side == "buy") {
      return coin2::exchange::feed::TRADE_BUY_SIDE;
    } else {
      return coin2::exchange::feed::TRADE_NO_SIDE;
    }
  }

  static fastfeed::proto::KlineInterval ParseKlineInterval(std::string_view interval) {
    static std::map<std::string, fastfeed::proto::KlineInterval> interval_map = {
      {"1", fastfeed::proto::KLINE_INTERVAL_1MIN},
      {"5", fastfeed::proto::KLINE_INTERVAL_5MIN},
      {"15", fastfeed::proto::KLINE_INTERVAL_15MIN},
      {"30", fastfeed::proto::KLINE_INTERVAL_30MIN},
      {"60", fastfeed::proto::KLINE_INTERVAL_1HOUR},
      {"1D", fastfeed::proto::KLINE_INTERVAL_1DAY}
    };

    auto iter = interval_map.find(std::string(interval));
    if (iter == interval_map.end()) {
      return fastfeed::proto::KLINE_INTERVAL_INVALID;
    }

    return iter->second;
  }

  static bool ParseKlineRest(
      const rapidjson::Value& msg,
      Klines* klines,
      fastfeed::proto::KlineInterval interval,
      int64_t timestamp) {
    int64_t open_timestamp = stoll(msg["t"].GetString()) * 1'000'000LL;
    auto result = klines->AddKline(open_timestamp);
    if (!result) return false;

    Kline* kline;
    bool freezed;
    std::tie(kline, freezed) = result.value();
    kline->open_timestamp = open_timestamp;
    kline->open = stod(msg["o"].GetString());
    kline->high = stod(msg["h"].GetString());
    kline->low = stod(msg["l"].GetString());
    kline->close = stod(msg["c"].GetString());
    kline->volume = stod(msg["v"].GetString());
    kline->timestamp = timestamp;
    kline->interval = interval;

    return freezed;
  }
};
}  // namespace impl

using impl::KorbitFeedUpdateParser;
